package org.aptech.hdax.controller;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

@Controller
@RequestMapping("/test")
public class TestController {

    @RequestMapping(value = "/anjian",method = RequestMethod.POST)
    public String anjian(String name, MultipartFile card, MultipartFile huzhao, HttpServletRequest request) throws Exception{
        System.out.println(name + "正在进行安检检查");

        //获取项目部署的绝对路径
        //c:...tupian/a.jpg
        String path = request.getServletContext().getRealPath("/tupian");
        if (card != null){
            File file = new File(path + File.separator + card.getOriginalFilename());
            card.transferTo(file);
        }

        if (huzhao != null){
            File file = new File(path + File.separator + huzhao.getOriginalFilename());
            huzhao.transferTo(file);
        }

        return "安检通过";
    }

    @RequestMapping("/sayHi/{username}/{date}")
    public String sayHello(@PathVariable String username,@PathVariable Date date) throws Exception{
        System.out.println(username + "打招呼了：" + date);
        System.out.println("你的名字有几个字？" + username.length());
        return "index";
    }

    @InitBinder
    public void initBinder(WebDataBinder binder) throws Exception{
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        CustomDateEditor dateEditor = new CustomDateEditor(dateFormat,true);
        binder.registerCustomEditor(Date.class,dateEditor);
    }
}
